﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Mst;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Inv
{
    public partial class frmMaintainWareHouse : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        public MstWarehouseEntity SelectedWarehouse { get; set; }
        public MstLocationEntity SelectedLocation;
        #endregion

        #region Constructor
        public frmMaintainWareHouse()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbIsDelete, typeof(DeleteFlag), typeof(string), false);
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbIsLocked, typeof(LockedFlag), typeof(string), false);
        }
        private void BindDataGrid(string WarehouseCode)
        {
            if (string.IsNullOrEmpty(WarehouseCode))
            {
                gvMain.DataSource = null;
            }
            DataTable dt = MstLocationBs.GetLocationByWarehouseCode(WarehouseCode);
            gvMain.DataSource = dt;
        }
        private void SetFields()
        {
            if (SelectedWarehouse == null)
            {
                return;
            }
            if (ThisActionType == ActionType.Modify)
            {
                btnSaveNew.Visible = false;

            }
            txtInvCode.Text = SelectedWarehouse.WarehouseCode;
            txtInvName.Text = SelectedWarehouse.WarehouseName;
            txtInvSeq.Text = SelectedWarehouse.Sequence.ToString();
            //txtInvManage.Text = SelectedWarehouse.Manager; 
            txtDesription.Text = SelectedWarehouse.Description;
            cmbIsLocked.SetSelectedValue(SelectedWarehouse.Locked);
            cmbIsDelete.SetSelectedValue(SelectedWarehouse.DeleteFlag);
            MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectedWarehouse.Manager);
            if (person != null)
            {
                txtInvManage.Text = person.NameCn;
                txtInvManage.Tag = person;
            }
                //UICommonFunctions.SetTextBoxPersonName(txtInvManage, SelectedWarehouse.Manager);
        }
        private void SetEntity()
        {
            if (SelectedWarehouse == null)
            {
                SelectedWarehouse = new MstWarehouseEntity();
            }
            SelectedWarehouse.WarehouseCode = txtInvCode.Text;
            SelectedWarehouse.WarehouseName = txtInvName.Text;
            SelectedWarehouse.Sequence = int.Parse(txtInvSeq.Text);
            SelectedWarehouse.Description = txtDesription.Text;
            if (cmbIsDelete.SelectedValue != null && cmbIsDelete.SelectedValue.ToString().Trim() != "")
            {
                SelectedWarehouse.DeleteFlag = CommonFunctions.ToInt32(cmbIsDelete.SelectedValue);
            }
            if (cmbIsLocked.SelectedValue != null && cmbIsLocked.SelectedValue.ToString().Trim() != "")
            {
                SelectedWarehouse.Locked = CommonFunctions.ToInt32(cmbIsLocked.SelectedValue);
            }
            if (!txtInvManage.Text.Equals(""))
            {
                SelectedWarehouse.Manager = ((MstPersonEntity)txtInvManage.Tag).PersonId;
            }
        }
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtInvCode.Text))
            {
                MessageBox.Show("仓库编码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtInvName.Text))
            {
                MessageBox.Show("仓库名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = MstWarehouseBs.InsertMstWarehouse(SelectedWarehouse);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = MstWarehouseBs.UpdateMstWarehouse(SelectedWarehouse);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        #endregion

        #region Events
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            if (SelectedWarehouse != null)
            {
                SetFields();
                BindDataGrid(SelectedWarehouse.WarehouseCode);
            }
            base.OnShown(e);
            if (ThisActionType == ActionType.New)
            {
                btnSaveNew.Enabled = true;
                gvMain.Enabled = false;
            }
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_SaveNew()
        {
            if (Save())
            {
                ThisActionType = ActionType.New;
                SelectedWarehouse = new MstWarehouseEntity();
                SetFields();
            }
        }

        private void ItemAddMore_Click(object sender, EventArgs e)
        {
            if (txtInvCode.Text.Equals(""))
            {
                MessageBox.Show("请输入仓库编号", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            frmMaintainLocationAdd frm = new frmMaintainLocationAdd();
            frm.SInvCode = txtInvCode.Text;
            DialogResult dr =  frm.ShowDialog();
            if (dr == DialogResult.OK&&!txtInvCode.Text.Equals(""))
            {
                BindDataGrid(txtInvCode.Text);
            }
        }


        private void ItemDelete_Click(object sender, EventArgs e)
        {
            if (SelectedLocation == null)
            {
                MessageBox.Show("请选择一个库位！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool successful = MstLocationBs.DeleteMstLocation(SelectedLocation);
            if (successful)
            {
                MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                BindDataGrid(SelectedWarehouse.WarehouseCode);
            }
            else
            {
                MessageBox.Show("删除失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtInvManage_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtInvManage.Text = frm.SelectedPerson.NameCn;
                txtInvManage.Tag = frm.SelectedPerson;
            }
            else
            {
                txtInvManage.Text = "";
                txtInvManage.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            SelectedLocation = new MstLocationEntity(dr);
        }
        #endregion

    }
}